<?php
include_once("Constants.php");
include_once("Entity/survey_answer_user.php");

class survey_answer_user_dao_base {

	function delete_survey_answer_user($id)
	{
		try
		{
			$dbh = new PDO(Constants::$connectionString, Constants::$username, Constants::$password);
			$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
			$dbh->beginTransaction();

			$stmt = $dbh->prepare("DELETE FROM survey_answer_user WHERE survey_answer_user_id = :survey_answer_user_id");
			$stmt->bindParam(':survey_answer_user_id', $id, PDO::PARAM_INT);
			$stmt->execute();
			
			$dbh->commit();
			$dbh = null;
		}
		catch(PDOException $e)
		{
			echo '<br />' . $e->getMessage();
			$dbh->rollback();
		}	
	}

	
	
	function get_survey_answer_user($id)
	{
		try
		{
			$dbh = new PDO(Constants::$connectionString, Constants::$username, Constants::$password);
			$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
			$dbh->beginTransaction();
			
			$stmt = $dbh->prepare("SELECT * FROM  survey_answer_user s WHERE s.survey_answer_user_id = :survey_answer_user_id");
			$stmt->bindParam(':survey_answer_user_id', $id, PDO::PARAM_INT);
			$stmt->execute();
			
			$s = null;
			if($row = $stmt->fetch())
			{
				$s = new survey_answer_user($row["survey_answer_user_id"],$row["survey_answer_id"],$row["user_id"],$row["created_date"]);
			}
			
			$dbh->commit();
			$dbh = null;
			return $s;
		}
		catch(PDOException $e)
		{
			echo '<br />' . $e->getMessage();
			$dbh->rollback();
		}	
	}
	
	function get_survey_answer_user_list()
	{
		try
		{
			$dbh = new PDO(Constants::$connectionString, Constants::$username, Constants::$password);
			$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
			$dbh->beginTransaction();

			$aos = array();
			$stmt = $dbh->query("SELECT * FROM survey_answer_user");
			$rows = $stmt->fetchAll();
			foreach($rows as $row)
			{
				$s = new survey_answer_user($row["survey_answer_user_id"],$row["survey_answer_id"],$row["user_id"],$row["created_date"]);
				$aos[] = $s;
			}

			$dbh->commit();
			$dbh = null;
			
			return $aos;
		}
		catch(PDOException $e)
		{
			echo '<br />' . $e->getMessage();
			$dbh->rollback();
		}	
	}

	function insert_survey_answer_user($s)
	{
		try
		{
			$dbh = new PDO(Constants::$connectionString, Constants::$username, Constants::$password);
			$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
			$dbh->beginTransaction();

			$stmt = $dbh->prepare("INSERT INTO survey_answer_user (survey_answer_id, user_id, created_date)
							VALUE(:survey_answer_id ,:user_id ,:created_date)");
							
							
			$stmt->bindParam(':survey_answer_id', $s->survey_answer_id);
			$stmt->bindParam(':user_id', $s->user_id);
			$stmt->bindParam(':created_date', $s->created_date);
			
			$stmt->execute();
			$id = $dbh->lastInsertId();	
			$dbh->commit();
			$dbh = null;

			return $id;
		}
		catch(PDOException $e)
		{
			echo '<br />' . $e->getMessage();
			$dbh->rollback();
		}	
	}

	function update_survey_answer_user($s)
	{
		try
		{
			$dbh = new PDO(Constants::$connectionString, Constants::$username, Constants::$password);
			$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
			$dbh->beginTransaction();

			$stmt = $dbh->prepare("UPDATE survey_answer_user s 
						SET 
						s.survey_answer_id = :survey_answer_id,
						s.user_id = :user_id,
						s.created_date = :created_date
						WHERE 
						s.survey_answer_user_id = :survey_answer_user_id");
			$stmt->bindParam(':survey_answer_id', $s->survey_answer_id);
			$stmt->bindParam(':user_id', $s->user_id);
			$stmt->bindParam(':created_date', $s->created_date);
			$stmt->bindParam(':survey_answer_user_id', $s->survey_answer_user_id);
			
			$stmt->execute();
			
			$dbh->commit();
			$dbh = null;
		}
		catch(PDOException $e)
		{
			echo '<br />' . $e->getMessage();
			$dbh->rollback();
		}	
	}

}
?>